package com.first.nell.me

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.first.nell.ProjectApplication
import com.first.nell.R
import com.first.nell.components.form.*
import com.first.nell.data.DataCustomizeContact
import com.first.nell.extend.copyFirstNellToFirstNellClipboard
import com.first.nell.extend.openFirstNellSystemFirstNellCall

class CustomerServiceAdapter(private val dataList: List<DataCustomizeContact>) :
    RecyclerView.Adapter<CustomerServiceAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var textFirstNellCustomerFirstNellServiceFirstNellTitle: TextView =
            view.findViewById(R.id.textCustomerServiceTitle)
        var textFirstNellCustomerFirstNellServiceFirstNellNumber: TextView =
            view.findViewById(R.id.textCustomerServiceNumber)
        var imageFirstNellCustomerFirstNellServiceFirstNellAction: ImageView =
            view.findViewById(R.id.imageCustomerServiceAction)
        var textFirstNellCustomerFirstNellServiceFirstNellAction: TextView =
            view.findViewById(R.id.textCustomerServiceAction)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_first_nell_adpter_customer_service, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: DataCustomizeContact = dataList[position]
        holder.textFirstNellCustomerFirstNellServiceFirstNellNumber.text = data.contact
        holder.textFirstNellCustomerFirstNellServiceFirstNellTitle.text = data.detail
        holder.imageFirstNellCustomerFirstNellServiceFirstNellAction.visibility = View.GONE
        when (data.type) {
            R.integer.ContactTypePhone -> {
                holder.imageFirstNellCustomerFirstNellServiceFirstNellAction.visibility =
                    View.VISIBLE
                holder.textFirstNellCustomerFirstNellServiceFirstNellAction.text =
                    ProjectApplication.context.getString(R.string.first_nell_dial)
                holder.imageFirstNellCustomerFirstNellServiceFirstNellAction.setImageResource(R.drawable.first_nell_phone_call)
                holder.textFirstNellCustomerFirstNellServiceFirstNellAction.setOnClickListener {
                    holder.textFirstNellCustomerFirstNellServiceFirstNellAction.context.openFirstNellSystemFirstNellCall(
                        data.contact
                    )
                }
                holder.imageFirstNellCustomerFirstNellServiceFirstNellAction.setOnClickListener {
                    holder.imageFirstNellCustomerFirstNellServiceFirstNellAction.context.openFirstNellSystemFirstNellCall(
                        data.contact
                    )
                }
            }
            R.integer.ContactTypeEmail -> {
                holder.textFirstNellCustomerFirstNellServiceFirstNellAction.text =
                    ProjectApplication.context.getString(R.string.first_nell_copy)
                holder.imageFirstNellCustomerFirstNellServiceFirstNellAction.setImageResource(R.drawable.first_nell_phone_copy)
                holder.imageFirstNellCustomerFirstNellServiceFirstNellAction.setOnClickListener {
                    holder.imageFirstNellCustomerFirstNellServiceFirstNellAction.context.copyFirstNellToFirstNellClipboard(
                        data.contact
                    )
                }
                holder.textFirstNellCustomerFirstNellServiceFirstNellAction.setOnClickListener {
                    holder.textFirstNellCustomerFirstNellServiceFirstNellAction.context.copyFirstNellToFirstNellClipboard(
                        data.contact
                    )
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}